if select(2, UnitClass("player")) ~= "DRUID" then return; end

local ringID, ShiftInfo, ShiftRingData = nil, {
	{r=0.8, g=0.4, b=1, s=24858}, -- Moonkin
	{r=0.3, g=1, b=0.4, s=33891}, -- Tree
	{r=1, g=0.3, b=0.3, s=9634, s2=5487}, -- Bear
	{r=0.2, g=0.7, b=1, s={40120, 33943, 783, 1066}}, -- Travel forms
	{r=1, g=1, b=0, s=768} -- Cat Form.
}, {name="Druid Forms", hotkey="BUTTON3"};

local function updateRingData(forceChange)
	local changed = (forceChange == true);

	-- Find ability names
	for i, form in ipairs(ShiftInfo) do
		if type(form.s) == "number" then
			local old = form.spell;
			form.spell = GetSpellInfo(GetSpellInfo(form.s)) or (form.s2 and GetSpellInfo(GetSpellInfo(form.s2)));
			changed = changed or (form.spell ~= old);
		elseif type(form.s) == "table" then
			form.spell = form.spell or {};
			for j, sid in ipairs(form.s) do
				if not (form.spell[j] and GetSpellInfo(ShiftInfo[i].spell[j])) then
					form.spell[j] = GetSpellInfo(sid);
					changed = changed or (form.spell[j] ~= nil);
				end
			end
		end
	end

	if not changed then
		return; -- Nothing to update
	end
	for i=1,#ShiftRingData do ShiftRingData[i] = nil;	end
	for i=1,#ShiftInfo do
		local e = ShiftInfo[i];
		if type(e.spell) == "string" then
			ShiftRingData[#ShiftRingData+1] = {"spell", e.spell, r=e.r, g=e.g, b=e.b};
		elseif type(e.spell) == "table" and next(e.spell) then
			local m = "/cancelform [noflyable]\n/cast";
			if e.spell[1] or e.spell[2] then m = m .. " [flyable,nocombat,noswimming][flying] " .. (e.spell[1] or e.spell[2]) .. ";";	end
			if e.spell[4] then m = m .. " [swimming] " .. e.spell[4] .. ";"; end
			if e.mount then m = m .. " [nocombat,nomod] " .. e.mount .. ";"; end
			if e.spell[3] then m = m .. " [outdoors] " .. e.spell[3] .. ";"; end
			if m:match(";$") then
				ShiftRingData[#ShiftRingData+1] = {"macrotext", m, r=e.r, g=e.g, b=e.b, preClick=e.preClick, xDSId=e.id};
			end
		end
	end
	if not ringID then
		ringID = OneRingLib:CreateRing("OPieDruidShift", ShiftRingData);
	else
		OneRingLib:SetRingData(ringID, ShiftRingData);
	end
end

EC_Register("PLAYER_LOGIN", "OPie.DruidShift", function() updateRingData(true); end);
EC_Register("SPELLS_UPDATE", "OPie.DruidShift", updateRingData);